// pages/order/comment/comment.js
const md5 = require('../../../utils/md5.js')
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        courseId: null,
        courseType: null,
        courseInfo: {},
        uploadImg: [],
        maxImg: 9,
        commentDetail: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            courseId: options.courseId,
            courseType: options.courseType
        })
        this.getCourseInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getCourseInfo: function() {
        const that = this
        wx.showLoading({
            title: '加载中'
        })
        let data = {
            online_id: this.data.courseId
        }
        if(this.data.courseType == 1){
            common.post('Online/online_list', data).then(function (res) {
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseInfo: {}
                    })
                } else {
                    that.setData({
                        courseInfo: res[0]
                    })
                }
            }).catch(function (e) { })
        }
        else{
            common.post('Online/down_online_list', data).then(function (res) {
                wx.hideLoading()
                if (res.code == 204) {
                    that.setData({
                        courseInfo: {}
                    })
                } else {
                    that.setData({
                        courseInfo: res[0]
                    })
                }
            }).catch(function (e) { })
        }
    },

    commentInput: function(e) {
        this.setData({
            commentDetail: e.detail.value
        })
    },

    objKeySort: function(obj) {
        let newkey = Object.keys(obj).sort()
        let newObj = {}
        for (let i = 0; i < newkey.length; i++) {
            newObj[newkey[i]] = obj[newkey[i]]
        }
        return newObj
    },



    uploadImg: function() {
        if (this.data.maxImg < 1) {
            wx.showToast({
                title: "最多上传9张图片",
                icon: 'none',
                duration: 1000
            })
            return
        }
        const that = this
        let data = {}
        let key = wx.getStorageSync('token')
        let sign = 'cds'
        let timestamp = Date.parse(new Date())
        data.key = key
        data.timestamp = timestamp
        data = this.objKeySort(data)
        for (let i in data) {
            sign += (i + data[i])
        }
        sign += 'cds'
        sign = md5(sign)
        data.sign = sign
        let uploadImg = this.data.uploadImg
        let maxImg = this.data.maxImg
        wx.chooseImage({
            count: maxImg, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                wx.showLoading({
                    title: '正在上传图片',
                })
                let tempFilePaths = res.tempFilePaths
                tempFilePaths.forEach(function(item, i) {
                    wx.uploadFile({
                        url: 'https://www.meiboshi.vip/api/Upimg/up_comment_img', //仅为示例，非真实的接口地址
                        filePath: item,
                        name: 'file',
                        formData: {
                            key: data.key,
                            timestamp: data.timestamp,
                            sign: data.sign
                        },
                        success(res) {
                            if (JSON.parse(res.data).code == 204) {
                                wx.hideLoading()
                                wx.showModal({
                                    title: '提示',
                                    content: JSON.parse(res.data).error,
                                    showCancel: false
                                })
                                return
                            }
                            maxImg--
                            uploadImg.push(JSON.parse(res.data).datas)
                            that.setData({
                                uploadImg: uploadImg,
                                maxImg: maxImg
                            })
                            if (i + 1 == tempFilePaths.length) {
                                wx.hideLoading()
                                wx.showToast({
                                    title: '上传图片成功',
                                    icon: 'none',
                                    duration: 1000
                                })
                            }
                        }
                    })
                })
            }
        })
    },

    deleteImg: function(e) {
        let uploadImg = this.data.uploadImg
        let maxImg = this.data.maxImg
        maxImg++
        uploadImg.splice(e.currentTarget.dataset.index, 1)
        this.setData({
            uploadImg: uploadImg,
            maxImg: maxImg
        })
    },

    confirmComment: function() {
        if (this.data.commentDetail == '') {
            wx.showToast({
                title: '请填写评论内容',
                icon: 'none',
                duration: 1000
            })
            return
        }
        wx.showLoading({
            title: '评论中...',
        })
        let data = {
            online_id: this.data.courseId,
            content: this.data.commentDetail
        }
        common.post('Online/add_comment_online', data).then(function(res) {
            wx.hideLoading()
            wx.showModal({
                title: '提示',
                content: '评论成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.navigateBack({
                            delta: 1
                        })
                    }
                }
            })
        }).catch(function(e) {})
    }
})